package com.hermit.qmst.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.hermit.qmst.MainActivity;
import com.hermit.qmst.MyApplication;
import com.hermit.qmst.R;
import com.hermit.qmst.service.AutoAnswerIntentService;
import com.hermit.qmst.tools.Common;
import com.hermit.qmst.tools.Log;

/**
 * Created by hermithermit on 15/2/18.
 */
public class AutoAnswerReceiver extends BroadcastReceiver {

    private View mCallPhoneWidget;

    @Override
    public void onReceive(Context context, Intent intent) {

        int state1 = ((TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE)).getCallState();
        String state2 = intent.getStringExtra(TelephonyManager.EXTRA_STATE);


        if (MyApplication.getInstance() != null){
            mCallPhoneWidget = MyApplication.getInstance().mCallPhoneWidget;
        }

        Log.i("AutoAnswerReceiver","state1 = " + state1 + " state2 = " + state2,true);
        try {
            if (state2.equals(TelephonyManager.EXTRA_STATE_RINGING) || state1 == 1){

                if (Common.mShowAutoAnswer && Common.mIsAutoAnswer){
                    context.startService(new Intent(context, AutoAnswerIntentService.class));

                }



                if (Common.mShowAutoAnswer){
                    //发送关闭拨打界面的广播
                    Intent intent1 = new Intent(MyApplication.IN_COMING_CALLING_ACTION);
                    context.sendBroadcast(intent1);
                    if (MainActivity.getInstance() != null) {
                        showCallPhoneWidget(context);
                    }
                }

                Common.mShowAutoAnswer = false;



            }else if (state2.equals(TelephonyManager.EXTRA_STATE_IDLE) || state1 == 0){

               if (MainActivity.getInstance() != null) {
                   clearCallPhoneWidget(context);
               }
            }



        }catch (Exception e){
            Common.mShowAutoAnswer = false;
        }
    }

    private void showCallPhoneWidget(final Context context){

        if(mCallPhoneWidget == null){
            return;
        }
        mCallPhoneWidget.findViewById(R.id.ic_cha).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clearCallPhoneWidget(context);
            }
        });

        if (Common.mCallName != null && Common.mCallName.length() > 0){
            ((TextView) mCallPhoneWidget.findViewById(R.id.tv_callphone)).setText(Common.mCallName);
        }else {
            ((TextView) mCallPhoneWidget.findViewById(R.id.tv_callphone)).setText(Common.mCallPhone);
        }

        WindowManager windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);

        WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        params.type =  WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY | WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
        ;
        params.flags =  WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL ;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.x = 0;
        params.y = -Common.mHeightPixels / 2;
        //显示
        windowManager.addView(mCallPhoneWidget, params);
    }

    //关闭遮挡条
    public void clearCallPhoneWidget(Context context){
        if (mCallPhoneWidget != null && mCallPhoneWidget.isShown()){
            WindowManager windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
            windowManager.removeView(mCallPhoneWidget);
        }
    }


}
